package com.wbu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wbu.entity.Admin;
import com.wbu.entity.Message;
import com.wbu.entity.Notice;
import com.wbu.entity.Order;
import com.wbu.entity.Park;
import com.wbu.entity.Users;
import com.wbu.service.AdminService;
import com.wbu.service.ParkService;



@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Autowired
	AdminService adminService;
	
	@Autowired
	ParkService parkService;
	


	@RequestMapping("/login")
	public String login(@ModelAttribute Admin admin,
						HttpSession session ,
						Model model) {
		admin = adminService.login(admin);
		if(null != admin) {
			session.setAttribute("LogAdmin", admin);
			return "/admin/index";
		} else {
			model.addAttribute("msg", "用户名或密码错误");
			return "/admin/login";
		}
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("LogAdmin");
		return "/admin/login";
	}
	
	@RequestMapping("/admin-list")
	public String getAllAdmin(Model model) {
		List<Admin> adminList = adminService.findAll();
		model.addAttribute("adminList", adminList);
		model.addAttribute("adminNum", adminList.size());
		return "/admin/admin-list";
	}
	
	@RequestMapping("/findAdminByName")
	public String findAdminByName(@RequestParam("nameTmp") String name,
							Model model) {
		List<Admin> adminList = adminService.findByName(name);
		model.addAttribute("adminList", adminList);
		model.addAttribute("adminNum", adminList.size());
		model.addAttribute("nameTmp", name);
		return "/admin/admin-list";
	}
	
	@RequestMapping("/admin-add")
	public String addAmin() {
		return "/admin/admin-add";
	}
	
	@ResponseBody
	@RequestMapping("/admin-save")
	public Map<String,String> adminSave(@ModelAttribute("admin") Admin admin) {
		//操作的结果需要封装返回前端
		Map<String,String> map = new HashMap<String,String>();
		map.put("res", "1");
		if(adminService.addAdmin(admin)){
			map.put("res", "0");
		}
		return map;
	}
	
	@RequestMapping("/change-info")
	public String changInfo(@ModelAttribute("adminId") int adminId,
			Model model) {
		//操作的结果需要封装返回前端
		Admin admin = adminService.findById(adminId);
		model.addAttribute("admin", admin);
		return "/admin/change-info";
	}
	
	@ResponseBody
	@RequestMapping("/updateAdmin")
	public Map<String,String> updateAdmin(Admin admin) {
		//操作的结果需要封装返回前端
		Map<String,String> map = new HashMap<String,String>();
		map.put("res", "1");
		if(adminService.updateAdmin(admin)){
			map.put("res", "0");
		}
		return map;
	}
	@ResponseBody
	@RequestMapping("/delAdmin")
	public Map<String,String> delAdmin(@RequestParam("adminId") int adminId) {
		//操作的结果需要封装返回前端
		Map<String,String> map = new HashMap<String,String>();
		map.put("res", "1");
		if(adminService.deleteAdmin(adminId)){
			map.put("res", "0");
		} 
		return map;
	}
	
	////////////////////////////////////关于车位开始
	@RequestMapping("/car-list")
	public String getAllPark(Model model) {
		List<Park> parkList = parkService.getAll();
		model.addAttribute("carList", parkList);
		model.addAttribute("carNum", parkList.size());
		return "car/car-list";
	}
	@RequestMapping("/findCarByKey")
	public String getAllPark(@RequestParam("keyTmp")String keyTmp,Model model) {
		List<Park> parkList = null;
		if(null!=keyTmp && !"".equals(keyTmp)) {
			parkList = parkService.getAllByKey(keyTmp);
		} else {
			parkList = parkService.getAll();
		}
		model.addAttribute("carList", parkList);
		model.addAttribute("carNum", parkList.size());
		model.addAttribute("keyTmp", keyTmp);
		return "car/car-list";
	}
	@RequestMapping("/car-save")
	@ResponseBody
	public Map<String,String> savePark(@ModelAttribute("car")Park park) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("res", "1");
		if(parkService.insertPark(park)) {
			map.put("res", "0");
		}
		return map;
	}
	@RequestMapping("/car-update")
	@ResponseBody
	public Map<String,String> updatePark(@ModelAttribute("car")Park park) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("res", "1");
		if(parkService.updatePark(park)) {
			map.put("res", "0");
		}
		return map;
	}
	
	@RequestMapping("/car-edit")
	public String editPark(@ModelAttribute("carId")int id,Model model) {
		Park parkInfo = parkService.getParkById(id);
		model.addAttribute("car",parkInfo);
		return "car/car-edit";
	}
	
	@RequestMapping("/delCar")
	@ResponseBody
	public Map<String,String> delCar(@ModelAttribute("carId")int id) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("res", "1");
		if(parkService.deletePark(id)) {
			map.put("res", "0");
		}
		return map;
	}
	@RequestMapping("/car-add")
	public String addPark() {
		return "car/car-add";
	}
	
	
	////////////////////////////////////////////////////管理员车位模块结束
	
	////////////////////////////////////////////////////订单模块

	//*****管理员留言管理******/

	///////////管理员留言管理结束/////////////
}
